package main

import (
	"fmt"
	"log"
)

func main() {
	maskTest()
}

func maskTest() {
	var n = 32
	var x []byte
	for i := 0; i < 16; i++ {
		x = append(x, getBit(n, i))
	}
	fmt.Println(x)
	r := getNumberFromBits(x)
	fmt.Println(r)
}

func getBit(number, location int) uint8 {
	x := (number >> location) & 1
	return uint8(x)
}

func setBit(number, pos, bit int) int {
	x := number & ^(1<<pos) | (bit << pos)
	return x
}

func getNumberFromBits(bits []uint8) int {
	var number, pos int
	for pos < 16 {
		number = setBit(number, pos, int(bits[pos]))
		pos++
	}
	return number
}

func test() {
	var intT int64
	intT = 1000000000
	fmt.Println(DecBin(intT))
	fmt.Println(len(DecBin(intT)))
	fmt.Println(DecBin(intT))
	fmt.Println(len(DecBin(intT)))
	n(intT)
}

func n(i int64) {
	if i>>30 >= 0 && i>>34 <= 0 {
		fmt.Println(10)
		return
	}
	if i>>40 >= 0 && i>>44 <= 0 {
		fmt.Println(13)
		return
	}
	if i>>60 >= 0 {
		fmt.Println(19)
		return
	}
	fmt.Println("有问题")
}

func DecBin(n int64) string {
	if n < 0 {
		log.Println("Decimal to binary error: the argument must be greater than zero.")
		return ""
	}
	if n == 0 {
		return "0"
	}
	s := ""
	for q := n; q > 0; q = q / 2 {
		m := q % 2
		s = fmt.Sprintf("%v%v", m, s)
	}
	return s
}
